 /*Generamos los dialog*/
 $(document).ready(function(){
     //Con JavaScript creamos los dialog
     $('#formGrpAdd').css("display","block");                        
     $('#formGrpAdd').dialog({                                                            
            modal: true,
            autoOpen:false,
            minWidth: 600,                        
            minHeight: 200,            
            draggable:false,
            resizable:false,
            show: "blind",
            hide: "slide",
            title:"Nuevo grupo",
            
            
            buttons: {
				"Crear": function() {
                                    mensaje=add($('#formGrpAdd').serialize());                                                                                
                                    $('#flex').flexReload();                                                                                                           					                                    
				},
				"Cancelar": function() {
                                    $('#mensaje').html('');     
                                    $( this ).dialog( "close" );
                                    $('#flex').flexReload();                                                                   
                                    //$( this ).dialog( "close" );
                                }
                                        				
			}
            
            
            });                                           
      $("#formGrpAdd").submit(function(){          
          return false;
      }); 
      /*Tabs de formUserAdd*/
      $(function() {
		$( "#tabsGrp" ).tabs({fx: {opacity: 'toggle'}});
	});
      
   
      
      
      
 });
 
 
 
 
 
 
 
 /***********************Funciones propias**************************************/           
 /*Agregar usuario*/
      function add(datos) {                            
          $.ajax({
             url: 'usuarios/ajaxUserAdd.php?'+datos,
             type: 'UPDATE',                          
             dataType: "html",
             success:function(data) {
                 $('#mensaje').html(data);                 
             }             
          });                    
          
      }
      /*Mostrar formulario agregar usuario*/
      function doGrpAdd(button, grid) {
          $('#mensaje').html('');//Borramos los mensajes
          $('.text').attr("value", "");//Dejamos en blanco los input
          $('#formGrpAdd').dialog("open");
       }
           // Funcion para Borrar 
    function doDelete(button, grid) {
       /*var total_count = $('.trSelected', grid).length;
       var deleted     = 0;
 
       $.each($('.trSelected', grid), function() {
          var editable = $('td:nth-child(1) div', this).html();

          // Comprobar si es editable o no...
          if ( "NO" == editable ) {
             alert("El usuario seleccionado no puede eliminarse");
             return;
          }
          // Borrar llamando al php en AJAX
          var id = this.id;
          $.ajax({
             url: 'AJAX.userDelete.php?entry='+ id ,
             type: 'DELETE',
             data: {},
             dataType: 'json',
             success: function() {
                deleted++;
                if (deleted == total_count) {
                    $('#flex1').flexReload();
                }
             }
          });
       });*/
          if($('.trSelected', grid).length > 0)
             {
          $( "#formUserDel" ).dialog({
            draggable:false,
            resizable:false,
            autoOpen:false,
            height:170,
            width:500,
            modal: true,
            show: "blind",
            hide: "slide",
            title:"Borrar usuario/s",
            buttons: {
                    "Aceptar": function() {                        
                            var total_count = $('.trSelected', grid).length;
                            var deleted     = 0;
                                var ids='';
                            $.each($('.trSelected', grid), function() {                                
                                // Borrar llamando al php en AJAX
                                var id = this.id;
                                ids+=','+id.substring(3, id.length);;                                                                
                            });
                            ids=ids.substring(1, ids.length);;                                                            
                            $.ajax({
                                    url: 'ajaxUserDel.php?entrys='+ ids ,
                                    type: 'DELETE',
                                    data: {},
                                    dataType: 'html',
                                    success: function() {                                        
                                        //Cuando termine recargamos la tabla
                                        $('#flex').flexReload();                                        
                                    }
                                });
                            $( this ).dialog( "close" );
                    },
                    "Cancelar": function() {
                            $( this ).dialog( "close" );
                            
                    }
            }
    });	
        $('#formUserDel').dialog("open");
             }
    }
       
     function doEdit(button, grid) {
       var total_count = $('.trSelected', grid).length;
       var deleted     = 0;
 
       $.each($('.trSelected', grid), function() {
          var editable = $('td:nth-child(2) div', this).html();

          // Bloquear o desbloquear llamando al php en AJAX
          $.ajax({
             url: 'AJAX.userLock.php?entry='+ editable ,
             type: 'UPDATE',
             data: {},
             dataType: 'json',
             success: function() {
                deleted++;
                if (deleted == total_count) {
                    $('#flex1').flexReload();
                }
             }
          });
       });
     }

     function doPasswd(button, grid) {
       var total_count = $('.trSelected', grid).length;
       var deleted     = 0;
       var id=null;
 
       $.each($('.trSelected', grid), function() {
          var editable = $('td:nth-child(1) div', this).html();

          // Comprobar si es editable o no...
          if ( "NO" == editable ) {
             alert("No puede modificar la clave del usuario seleccionado");
             return;
          }
          id = this.id.substring(3);
       });
       if (null!=id) {
          document.getElementById('modalBackground2').style.visibility='visible';
          document.getElementById('pass_login').value=id;
          document.getElementById('pass_1').value="";
          document.getElementById('pass_2').value="";
          jQuery('.errormsg2').text("");
          jQuery("#ajaxpopform-success2").css('display','none'); 
          jQuery(".ajaxpopformwrapper2").css('display','block'); 
          jQuery('#ajaxpopform2').center();
          jQuery('#ajaxpopform2').fadeIn(500);
          jQuery(button).blur();
       }
     }


     function doGroups(button, grid) {
       var total_count = $('.trSelected', grid).length;
       var deleted     = 0;
       var id=null;
 
       $.each($('.trSelected', grid), function() {
          var editable = $('td:nth-child(1) div', this).html();
          id = this.id.substring(3);
       });
       if (null!=id) {
          $.getJSON("AJAX.userGetGroups.php",{user: id}, function(j){
					var options = '';
					for (var i = 0; i < j.length; i++) {
						options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
					}
					$("#firstList").html(options);
          });
          $.getJSON("AJAX.userGetGroups.php",{user: id, exclude: 'yes'}, function(j){
					var options = '';
					for (var i = 0; i < j.length; i++) {
						options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
					}
					$("#secondList").html(options);
          });
          document.getElementById('modalBackground3').style.visibility='visible';
          document.getElementById('grp_login').value=id;
          jQuery('.errormsg3').text("");
          jQuery("#ajaxpopform-success3").css('display','none'); 
          jQuery(".ajaxpopformwrapper3").css('display','block'); 
          jQuery('#ajaxpopform3').center();
          jQuery('#ajaxpopform3').fadeIn(500);
          jQuery(button).blur();
       }

     }


         